package com.example.demo.controller;

import com.example.demo.annotation.RequestAuthCheck;
import com.example.demo.config.ConstantConfig;
import com.example.demo.entity.BaseEntity;
import com.example.demo.entity.OperLog;
import com.example.demo.entity.dto.PageDto;
import com.example.demo.entity.vo.PageVo;
import com.example.demo.entity.vo.ResponseResult;
import com.example.demo.enums.StatusCodeEnum;
import com.example.demo.service.LogService;
import com.example.demo.util.MyExcelUtil;
import com.example.demo.util.NormalUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description：日志控制器.
 * @author：lw
 * @create_time：2024/1/19
 **/


@Tag(name="日志接口")
@ApiSupport(order = 6)
@RestController
@RequestMapping(value = "/admin/log")
public class LogController {
    
    @Autowired
    LogService logService;

    private final String menuName="log";

    @Operation(summary = "删除")
    @ApiOperationSupport(order = 1 ,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.delete)
    @PostMapping(value = "/deleteByIds")
    public ResponseResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
//        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
//        updateWrapper.in("id",ids);
//        updateWrapper.set("is_deleted",1);
//        return  ResponseResult.build(userService.update(updateWrapper),StatusCodeEnum.delete_success);
        return  ResponseResult.build(logService.removeByIds(ids),StatusCodeEnum.delete_success);
    }

    @Operation(summary = "获取详情")
    @ApiOperationSupport(order = 2 ,author = "lw")
    @GetMapping(value = "/detail")
    public ResponseResult<OperLog> getDetail(Long id) {
        return  ResponseResult.build(logService.getById(id),StatusCodeEnum.success);
    }

    @Operation(summary = "分页查询")
    @ApiOperationSupport(order = 3,author = "lw")
    @PostMapping(value = "/page")
    public ResponseResult<PageVo<OperLog>> getPage(@RequestBody PageDto<OperLog> pageDto) {
        return  ResponseResult.build(logService.getPage(pageDto), StatusCodeEnum.query_success);
    }
    
    @Operation(summary = "导出")
    @ApiOperationSupport(order = 4,author = "lw")
    @RequestAuthCheck(menuName = menuName,action = ConstantConfig.export)
    @PostMapping(value = "/export")
    public void export(@RequestBody PageDto<OperLog> pageDto, HttpServletResponse response) {
        PageVo<OperLog> pageVo=logService.getPage(pageDto);
        MyExcelUtil.export(response,pageVo, NormalUtil.mergeMaps(OperLog.getExcelCols(), BaseEntity.getExcelCols()),OperLog.entityTitle);
    }
}
